package com.wsjc.common.redis.spring;

import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.stereotype.Component;

import com.wsjc.common.redis.RedisUtils;

import redis.clients.jedis.Jedis;






/**
 * 
 * ClassName: RedisCache <br/>
 * Function: 根据SPring API 自定义一个缓存类 ，实现Redis 缓存。<br/>
 */

@Component("redisCache")
public class RedisCache implements Cache {
	/**缓存的命名属性**/
	private String name;

	@Autowired
	private static RedisUtils cache ;
	
	/**
	 * 清空所有的缓存 
	 */
	public void clear() {
		Jedis jedis = cache.instanceJedisFactory("agent");
		cache.flushAll(jedis, "default");
		cache.releaseResource(jedis);
	}

	@Override
	public void evict(Object key) {
		Jedis jedis = cache.instanceJedisFactory("agent");
		cache.del(jedis, key);
		cache.releaseResource(jedis);
	}

	/**
	 * 根据Key值获得缓存数据
	 */
	public ValueWrapper get(Object key) {
		ValueWrapper result = null;
		Jedis jedis = cache.instanceJedisFactory("agent");
		Object thevalue = cache.get(jedis, key);
		cache.releaseResource(jedis);
		if (thevalue != null) {
			result = new SimpleValueWrapper(thevalue);
		}
		return result;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Object getNativeCache() {
		return cache;
	}
	
	/**添加缓存*/
	public void put(Object arg0, Object arg1) {
		Jedis jedis = cache.instanceJedisFactory("agent");
		cache.save(jedis, arg0, arg1,20000);
		cache.releaseResource(jedis);
		
	}

	public RedisCache() {
	}

	public RedisCache(String name) {
		this.name = name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public <T> T get(Object arg0, Class<T> arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> T get(Object arg0, Callable<T> arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ValueWrapper putIfAbsent(Object arg0, Object arg1) {
		// TODO Auto-generated method stub
		return null;
	}

}
